package com.dykj.fireFighting.web.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonKeyEdit {
    private static String updateObjKey(String jsonStr){
        //转换后的字符串
        String str = jsonStr;

        //满足json字符串key的正则
        String regx = "\"\\w+\":";

        //1.将正在表达式封装成对象Patten 类来实现
        Pattern pattern = Pattern.compile(regx);

        //2.将字符串和正则表达式相关联
        Matcher matcher = pattern.matcher(jsonStr);

        while(matcher.find()){

            System.out.println(matcher.start()+"--"+matcher.end()+" : "+matcher.group());

            str = str.replaceFirst(matcher.group(),camelCase(matcher.group()));
        }

        return str;
    }

    public static String camelCase(String str){
        String camelCase = "";
        String [] arr = str.split("_");
        List<String> list = new ArrayList<String>();
        //将数组中非空字符串添加至list
        for(String a : arr){
            if(a.length() > 0){
                list.add(a);
            }
        }
        for(int i=0;i<list.size();i++){
            if(i>0){    //后面单词首字母大写
                char c = list.get(i).charAt(0);
                String s = String.valueOf(c).toUpperCase() + list.get(i).substring(1).toLowerCase();
                camelCase+=s;
            }else{  //首个单词小写
                camelCase+=list.get(i).toLowerCase();
            }
        }
        return camelCase;
    }
}
